package com.atguigu.gulimall.member.controller;

import java.util.Arrays;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.atguigu.common.exception.BizCodeEnume;
import com.atguigu.gulimall.member.Exception.UserUniqueException;
import com.atguigu.gulimall.member.Vo.MemberVo;
import com.atguigu.gulimall.member.Vo.SocialUser;
import com.atguigu.gulimall.member.Vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.member.entity.MemberEntity;
import com.atguigu.gulimall.member.service.MemberService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;



/**
 * 会员
 *
 * @author yangheng
 * @email sunlightcs@gmail.com
 * @date 2023-03-25 10:54:23
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    /**
     * 社交登录
     * @param
     * @return
     */
    @PostMapping("/social/login")
    public R socialLogin(@RequestBody SocialUser socialUser) throws Exception {
        MemberEntity memberEntity=memberService.socialLogin(socialUser);
         if(memberEntity!=null){
             return R.ok().setData(memberEntity);
         }else{
             return R.error(BizCodeEnume.UNKNOW_EXCEPTION.getCode(),BizCodeEnume.UNKNOW_EXCEPTION.getMsg());
         }

    }




    /**
     * 登录验证功能
     * @param memberVo
     * @return
     */
    @PostMapping("/login")
    public R login(@RequestBody MemberVo memberVo){
        MemberEntity memberEntity=memberService.login(memberVo);
         if(memberEntity==null){
             return  R.error(BizCodeEnume.Login_ERR.getCode(),BizCodeEnume.Login_ERR.getMsg());
         }
       return R.ok().setData(memberEntity);
    }








    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("member:member:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


     @RequestMapping("/regist")
     public R regist(@RequestBody UserVo userVo){

        try{
            memberService.registMember(userVo);
        }catch (UserUniqueException e){
           return R.error(1,e.getMessage());
        }
        return R.ok();
    }




    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
 //   @RequiresPermissions("member:member:info")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
  //  @RequiresPermissions("member:member:save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
   // @RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
   // @RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
